﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.BL;
using SES.CMS.DO;
using System.Data;

namespace SES.CMS
{
    public partial class PTTD : System.Web.UI.MasterPage
    {
       DataTable dtx = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnPTTT_Click(object sender, EventArgs e)
        {
            divSearch.Visible = false;
            divProduct.Visible = true;
            DataTable dt = new DataView(new cmsProductCategoryBL().SelectAll(), "ProductCategoryID = 7", "", DataViewRowState.CurrentRows).ToTable();
            if (dt.Rows.Count > 0)
            {
                dtx.Merge(dt);
                DataView dtv = new DataView(dtx);
                dtv.RowFilter = "ProductCategoryID = " + dt.Rows[0]["ProductCategoryID"];
            }

            rptTabCateShowProduct.DataSource = dt;
            rptTabCateShowProduct.DataBind();

            rptTabCateBindDiv.DataSource = dt;
            rptTabCateBindDiv.DataBind();

            rptProductDetail.Visible = false;
        }

        protected void btnTDT_Click(object sender, EventArgs e)
        {
            divSearch.Visible = false;
            divProduct.Visible = true;
            DataTable dt = new DataView(new cmsProductCategoryBL().SelectAll(), "ProductCategoryID = 6", "", DataViewRowState.CurrentRows).ToTable();
            if (dt.Rows.Count > 0)
            {
                dtx.Merge(dt);
                DataView dtv = new DataView(dtx);
                dtv.RowFilter = "ProductCategoryID = " + dt.Rows[0]["ProductCategoryID"];
            }
            rptTabCateShowProduct.DataSource = dt;
            rptTabCateShowProduct.DataBind();

            rptTabCateBindDiv.DataSource = dt;
            rptTabCateBindDiv.DataBind();

            rptProductDetail.Visible = false;
        }

        protected void rptTabCateShowProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;

            }
        }
        protected void rptTabCateBindDiv_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;
                Repeater rptProduct = (Repeater)e.Item.FindControl("rptProduct");

                rptProduct.DataSource = new cmsProductBL().SelectByProductCategory(int.Parse(drv["ProductCategoryID"].ToString()));
                rptProduct.DataBind();
            }
        }
        protected void rptProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.Equals("product_click"))
            {
                int productID = int.Parse(e.CommandArgument.ToString());
                rptProductDetail.Visible = true;
                rptProductDetail.DataSource = new cmsProductBL().SelectByPK(productID);
                rptProductDetail.DataBind();
            }
        }
        // Nut Tìm kiếm
        protected void btnSearch_OnClick(object sender, EventArgs e)
        {
            divSearch.Visible = true;
            divProduct.Visible = false;
            rptProductDetail.Visible = false;
            DataTable dt = new cmsProductBL().SelectByProductCategoryAndKeyword(0, txtSearch.Text.Trim());
            rptSearch.DataSource = dt;
            rptSearch.DataBind();
        }
        protected void rptSearch_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.Equals("product_click"))
            {
                int productID = int.Parse(e.CommandArgument.ToString());
                rptProductDetail.Visible = true;
                rptProductDetail.DataSource = new cmsProductBL().SelectByPK(productID);
                rptProductDetail.DataBind();
            }
        }
        // End Nut Tìm kiếm

        //Bind Images of Product(In Tab Images);
        protected void rptProductDetail_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;
                int productID = int.Parse(drv["ProductID"].ToString());

                Repeater rptImageProduct = (Repeater)e.Item.FindControl("rptImageProduct");

                rptImageProduct.DataSource = new cmsImagesBL().SelectBySanPhamID(productID);
                rptImageProduct.DataBind();

                Repeater rptProductProperties = (Repeater)e.Item.FindControl("rptProductProperties");  
               rptProductProperties.DataSource =  new cmsProductPropertyBL().SelectByProductID(productID);
               rptProductProperties.DataBind();
            }
        }
        protected void rptImageProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Panel pnalImage = (Panel)e.Item.FindControl("pnalImage");
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (e.Item.ItemIndex == 0)
                {
                    pnalImage.CssClass = "big-image-box";
                }
                else
                {
                    pnalImage.CssClass = "small-image-box";
                }
            }
        }
    }
}